package cn.fuxi2022.accounts.activity.service.impl;

import cn.fuxi2022.accounts.activity.mapper.GroupActivityMapper;
import cn.fuxi2022.accounts.activity.service.GroupActivityCountService;
import cn.fuxi2022.accounts.activity.service.GroupActivityService;
import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.model.empty.accounts.GroupActivity;
import cn.fuxi2022.model.resp.PageUtils;
import cn.fuxi2022.qiniu.utils.QiNiuUtils;
import cn.fuxi2022.rabbitmq.appletsPay.constants.ActivityConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 社团活动表 服务实现类
 * </p>
 *
 * @author Fuxi
 * @since 2022-10-19 08:42:14
 */
@Service
public class GroupActivityServiceImpl extends ServiceImpl<GroupActivityMapper, GroupActivity> implements GroupActivityService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 1.构建搜索条件
        LambdaQueryWrapper<GroupActivity> wrapper = new LambdaQueryWrapper<>();
        // 按照创建日期排序
        wrapper.orderByDesc(GroupActivity::getGmtCreate);

        // 2.获取请求参数，判断是否进行搜索查询
        // 搜索标题或描述
        if (ObjectUtils.isNotEmpty(params.get("search"))) {
            String search = String.valueOf(params.get("search"));
            wrapper.like(GroupActivity::getTitle, search).or().like(GroupActivity::getDescription, search);
        }
        // 搜索状态
        if (ObjectUtils.isNotEmpty(params.get("status"))) {
            int status = Integer.parseInt(String.valueOf(params.get("status")));

            if (status == 0) {
                wrapper.eq(GroupActivity::getStatus, 0);
            } else if (status == 1) {
                wrapper.eq(GroupActivity::getStatus, 1);
            } else if (status == 2) {
                wrapper.eq(GroupActivity::getStatus, 2);
            }
        }

        // 搜索类型
        if (ObjectUtils.isNotEmpty(params.get("type"))) {
            int status = Integer.parseInt(String.valueOf(params.get("type")));

            if (status == 0) {
                wrapper.eq(GroupActivity::getType, 0);
            } else if (status == 1) {
                wrapper.eq(GroupActivity::getType, 1);
            } else if (status == 2) {
                wrapper.eq(GroupActivity::getType, 2);
            } else if (status == 3) {
                wrapper.eq(GroupActivity::getType, 3);
            }
        }

        // 3.工具分页
        IPage<GroupActivity> page = this.page(
                new Query<GroupActivity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void startActivity(String id, String endTime) {
        Date date;
        try {
            date = dateFormat.parse(endTime);
        } catch (ParseException e) {
            throw new SmartGroupException("结束时间非法，Java格式为：yyyy年MM月dd日 HH:mm:ss");
        }

        // 判断时间是否是未来的时间
        LocalDateTime localTime = LocalDateTime.parse(endTime, dtf);
        if (!LocalDateTime.now().isBefore(localTime)) {
            throw new SmartGroupException("结束时间非法，结束时间必须在将来，而不是过去");
        }

        // 判断修改的活动是否是未开始，进行中和结束的活动不能修改
        GroupActivity groupActivity = getById(id);
        if (groupActivity.getStatus() != 0) {
            throw new SmartGroupException("要开始的活动正在进行中或已经结束，无法开始");
        }

        // 开始活动
        groupActivity.setEndTime(date);
        groupActivity.setStatus(1);
        updateById(groupActivity);

        // 发送活动ID至交换机
        rabbitTemplate.convertAndSend(ActivityConstants.GROUP_ACTIVITY_TTL_EXCHANGE, null, groupActivity.getId());
    }

    @Autowired
    private GroupActivityCountService groupActivityCountService;

    @Override
    public void stopActivity(String id) {
        // 判断修改的活动是否是未开始，未开始的活动不能修改
        GroupActivity groupActivity = getById(id);
        if (groupActivity.getStatus() != 1) {
            return;
        }
        // 设置活动结束
        groupActivity.setStatus(2);
        // 录入实际结束活动的时间
        groupActivity.setEndTime(new Date());
        updateById(groupActivity);
        // 销毁签到链接
        if (ObjectUtils.isNotEmpty(groupActivity.getRegisterImageUrl())) {
            QiNiuUtils.deleteFileFromQiniu(groupActivity.getRegisterImageUrl());
        }
        // 触发统计
        groupActivityCountService.groupActivityRefresh();
        groupActivityCountService.groupActivityRefresh();
    }
}
